package com.yzqp.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yzqp.common.result.*;
import com.yzqp.common.util.BeanUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import com.yzqp.sys.bean.*;
import com.yzqp.sys.service.*;
import com.yzqp.sys.form.*;
import com.yzqp.sys.vo.*;

/**
 * Permission controller
 */
@Api("Controller")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    public PermissionService _service;

    @ApiOperation(value = "全部列表")
    @GetMapping(value = "/all")
    public WebJsonResult all() {
        List<PermissionBean> list = _service.findAllList();
        // 响应数据格式化
        List<PermissionVo> res = BeanUtil.copyList(list, PermissionVo.class);
        return WebJsonResultUtil.success(res);
    }

    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/page")
    public WebJsonResult page(@Valid @RequestBody PermissionPageForm pageForm, BindingResult result) {
        // 校验数据是否正确
        if (result.hasErrors()) {
            String errMsg = result.getFieldError().getDefaultMessage();
            return WebJsonResultUtil.error(errMsg);
        }
        // 强制转换
        PermissionBean entity = BeanUtil.copy(pageForm, PermissionBean.class);
        IPage<PermissionBean> res = _service.findSinglePage(entity, pageForm.getCurrent(), pageForm.getSize());
        // 响应数据格式化
        res.convert(e -> BeanUtil.copy(e, PermissionVo.class));
        return WebJsonResultUtil.success(res);
    }

    @ApiOperation(value = "获取详情（根据id）")
    @GetMapping(value = "/get/{id}")
    public WebJsonResult get(@ApiParam("编号") @PathVariable Long id) {
        PermissionBean entity = _service.get(id);
        // 响应数据格式化
        PermissionVo res = BeanUtil.copy(entity, PermissionVo.class);
        return WebJsonResultUtil.success(res);
    }

    @ApiOperation(value = "添加记录")
    @PostMapping(value = "/add")
    public WebJsonResult add(@Valid @RequestBody PermissionSaveForm saveForm, BindingResult result) {
        // 校验数据是否正确
        if (result.hasErrors()) {
            String errMsg = result.getFieldError().getDefaultMessage();
            return WebJsonResultUtil.error(errMsg);
        }
        // 强制转换
        PermissionBean entity = BeanUtil.copy(saveForm, PermissionBean.class);
        _service.insert(entity);
        return WebJsonResultUtil.success();
    }

    @ApiOperation(value = "更新记录")
    @PutMapping(value = "/update")
    public WebJsonResult update(@Valid @RequestBody PermissionSaveForm saveForm, BindingResult result) {
        // 校验数据是否正确
        if (result.hasErrors()) {
            String errMsg = result.getFieldError().getDefaultMessage();
            return WebJsonResultUtil.error(errMsg);
        }
        // 强制转换
        PermissionBean entity = BeanUtil.copy(saveForm, PermissionBean.class);
        _service.update(entity);
        return WebJsonResultUtil.success();
    }

    @ApiOperation(value = "保存记录")
    @PostMapping(value = "/save")
    public WebJsonResult save(@Valid @RequestBody PermissionSaveForm saveForm, BindingResult result) {
        // 校验是否错误
        if (result.hasErrors()) {
            String errMsg = result.getFieldError().getDefaultMessage();
            return WebJsonResultUtil.error(errMsg);
        }
        // 强制转换
        PermissionBean entity = BeanUtil.copy(saveForm, PermissionBean.class);
        if (!StringUtils.isEmpty(entity.getId())) {
            _service.update(entity);
        } else {
            _service.insert(entity);
        }
        return WebJsonResultUtil.success();
    }

    @ApiOperation(value = "删除记录")
    @DeleteMapping(value = "/delete/{id}")
    public WebJsonResult delete(@ApiParam("编号") @PathVariable Long id) {
        _service.deleteById(id);
        return WebJsonResultUtil.success();
    }

    @ApiOperation(value = "全部列表(多级权限)")
    @GetMapping(value = "/allList")
    public WebJsonResult allList() {
        List<PermissionBean> list = _service.findAllPermissionListByTree();
        // 响应数据格式化
        List<PermissionVo> res = BeanUtil.copyList(list, PermissionVo.class);
        return WebJsonResultUtil.success(res);
    }
}
